home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / pcl4c40.zip / XYMODEM.C < prev    next >
Text File  |  1993-09-21  |  7KB  |  228 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <io.h>
  6. #include <sys\types.h>
  7. #include <sys\stat.h>
  8.  
  9. #include "pcl4c.h"
  10. #include "ascii.h"
  11. #include "term_io.h"
  12. #include "xypacket.h"
  13. #include "xymodem.h"
  14.  
  15. #define FALSE 0
  16. #define TRUE !FALSE
  17.  
  18.  
  19. int TxyModem(
  20.    int Port,            /* COM port [0..3] */
  21.    char Filename[],     /* filename buffer */
  22.    char Buffer[],       /* 1024 byte data buffer */
  23.    int OneKflag,        /* if TRUE, use 1K blocks when possible */
  24.    int BatchFlag)       /* if TRUE, send filename in packet 0 */
  25. {int i, k;
  26.  int Code;
  27.  int Handle;         /* file Handle */
  28.  char c;
  29.  int p;
  30.  char PacketType;
  31.  char PacketNbr;
  32.  int  PacketSize;
  33.  int  FirstPacket;
  34.  unsigned short CheckSum;
  35.  int Number1K = 0;       /* total # 1K packets */
  36.  int Number128 = 0;      /* total # 128 byte packets */
  37.  char NCGchar = NAK;
  38.  long FileSize;
  39.  char temp[81];
  40.  int EmptyFlag = FALSE;
  41.  /* begin */
  42.  if(BatchFlag) if(Filename[0]=='\0') EmptyFlag = TRUE;
  43.  if(!EmptyFlag)
  44.      {/* Filename is not empty */
  45.       EmptyFlag = FALSE;
  46.       Handle = open(Filename,O_RDONLY|O_BINARY,S_IREAD);
  47.       if(Handle<0)
  48.           {strcpy(temp,"Cannot open ");
  49.            strcat(temp,Filename);
  50.            DisplayLine(temp,NULL,0);
  51.            return(FALSE);
  52.           }
  53.      }
  54.  DisplayLine("XYMODEM send: waiting for Receiver ",NULL,0);
  55.  while(SioKeyPress()) SioKeyRead();
  56.  /* compute # blocks */
  57.  if(!EmptyFlag)
  58.      {FileSize = filelength(Handle);
  59.       if(OneKflag) Number1K = (int) (FileSize / 1024L);
  60.       Number128 = 1 + (int) ((FileSize -1024L*(long)Number1K -1L) / 128L);
  61.       sprintf(temp,"%d 1024 & %d 128 byte packets",Number1K,Number128);
  62.       DisplayLine(temp,NULL,0);
  63.      }
  64.  else
  65.      {/* empty file */
  66.       Number128 = 0;
  67.       Number1K = 0;
  68.       /*DisplayLine("Empty File",NULL,0);*/
  69.      }
  70.  /* clear comm port ( there may be several NAKs queued up ) */
  71.  SioRxFlush(Port);
  72.  /* get receivers start up NAK, 'C', or 'G' */
  73.  if(!TxStartup(Port,&NCGchar)) return(FALSE);
  74.  /* loop over all packets */
  75.  if(BatchFlag) FirstPacket = 0;
  76.  else FirstPacket = 1;
  77.  for(p=FirstPacket;p<=Number1K+Number128;p++)
  78.        {/* user aborts ? */
  79.         if(SioKeyPress()) if((char)SioKeyRead()==CAN)
  80.           {TxCAN(Port);
  81.            DisplayLine("*** Canceled by USER ***",NULL,0);
  82.            return(FALSE);
  83.           }
  84.         /* issue message */
  85.         sprintf(temp,"Packet %d",p);
  86.         DisplayLine(temp,NULL,0);
  87.         /* load up Buffer */
  88.         if(p==0)
  89.               {
  90.                /* Filename packet ! */
  91.                PacketSize = 128;
  92.                k = 0;
  93.                for(i=0;i<strlen(Filename);i++) Buffer[k++] = Filename[i];
  94.                Buffer[k++] = '\0';
  95.                sprintf(temp,"%ld",FileSize);
  96.                for(i=0;i<strlen(temp);i++) Buffer[k++] = temp[i];
  97.                while(k<128) Buffer[k++] = '\0';
  98.               }
  99.         else /* p > 0 */
  100.               {/* DATA Packet: use 1K or 128 byte block ? */
  101.                if(p<=Number1K) PacketSize = 1024;
  102.                else PacketSize = 128;
  103.                /* read next block from disk */
  104.                Code = read(Handle,Buffer,PacketSize);
  105.                if(Code<=0)
  106.                      {SayError(Port,"Error on disk read");
  107.                       return(FALSE);
  108.                      }
  109.                for(i=Code;i<PacketSize;i++) Buffer[i] = 0x1a;
  110.               }
  111.         /* send this packet */
  112.         if(!TxPacket(Port,p,PacketSize,Buffer,NCGchar)) return(FALSE);
  113.         SioDelay(5);
  114.         /* must 'restart' after non null packet 0 */
  115.         if(!EmptyFlag&&(p==0)) TxStartup(Port,&NCGchar);
  116.        } /* end -- for(p) */
  117.  /* done if empty packet 0 */
  118.  if(EmptyFlag)
  119.         {DisplayLine("Batch transfer complete",NULL,0);
  120.          return(TRUE);
  121.         }
  122.  /* all done. send EOT up to 10 times */
  123.  close(Handle);
  124.  if(!TxEOT(Port))
  125.      {SayError(Port,"EOT not acknowledged");
  126.       return(FALSE);
  127.      }
  128.  DisplayLine("Transfer Complete",NULL,0);
  129.  return(TRUE);
  130. } /* end -- TxyModem */
  131.  
  132. int RxyModem(
  133.    int Port,            /* COM port [0..3] */
  134.    char Filename[],     /* filename buffer */
  135.    char Buffer[],       /* 1024 byte data buffer */
  136.    char NCGchar,        /* NAK, 'C', or 'G' */
  137.    int BatchFlag)       /* if TRUE, get filename from packet 0 */
  138. {int i;
  139.  int Handle;         /* file Handle */
  140.  int p;              /* packet index */
  141.  int Code;           /* return code */
  142.  int FirstPacket;
  143.  char PacketNbr;
  144.  int PacketSize;           /* 128 or 1024 */
  145.  long FileSize;
  146.  char temp[81];
  147.  int  EOTflag = FALSE;
  148.  /* begin */
  149.  EOTflag = FALSE;
  150.  DisplayLine("XYMODEM Receive: Waiting for Sender ",NULL,0);
  151.  while(SioKeyPress()) SioKeyRead();
  152.  /* clear comm port */
  153.  SioRxFlush(Port);
  154.  /* Send NAKs, 'C's, or 'G's */
  155.  if(!RxStartup(Port,&NCGchar)) return(FALSE);
  156.  /* open file unless BatchFlag is on */
  157.  if(BatchFlag) FirstPacket = 0;
  158.  else
  159.      {/* start with packet 1 */
  160.       FirstPacket = 1;
  161.       /* open file passed in Filename[] for write */
  162.       Handle = open(Filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IWRITE);
  163.       if(Handle<0)
  164.           {strcpy(temp,"Cannot open ");
  165.            strcat(temp,Filename);
  166.            DisplayLine(temp,NULL,0);
  167.            return(FALSE);
  168.           }
  169.      }
  170.  /* get each packet in turn */
  171.  for(p=FirstPacket;;p++)
  172.      {/* user aborts ? */
  173.       if(SioKeyPress()) if((char)SioKeyRead()==CAN)
  174.         {TxCAN(Port);
  175.          return(FALSE);
  176.         }
  177.       /* issue message */
  178.       sprintf(temp,"Packet %d",p);
  179.       DisplayLine(temp,NULL,0);
  180.       /* get next packet */
  181.       if(!RxPacket(Port,p,&PacketSize,Buffer,NCGchar,&EOTflag)) return(FALSE);
  182.       if(p==0)
  183.         {/* copy Filename */
  184.          strcpy(Filename,Buffer);
  185.          /* done if null packet 0 */
  186.          if(Filename[0]=='\0')
  187.                 {DisplayLine("Batch Transfer Complete",NULL,0);
  188.                  return(TRUE);
  189.                 }
  190.         }
  191.       /* all done if EOT was received */
  192.       if(EOTflag)
  193.           {close(Handle);
  194.            DisplayLine("Transfer Complete",NULL,0);
  195.            return(TRUE);
  196.           }
  197.       /* process packet */
  198.       if(p==0)
  199.           {/* open file using filename in packet 0 */
  200.            Handle = open(Filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IWRITE);
  201.            if(Handle<0)
  202.                 {strcat(Buffer," -- open failed");
  203.                  DisplayLine(Buffer,NULL,0);
  204.                  return(FALSE);
  205.                 }
  206.            /* get file length */
  207.            FileSize = atol(&Buffer[1+strlen(Buffer)]);
  208.            /* must 'restart' after packet 0 */
  209.            RxStartup(Port,&NCGchar);
  210.           }
  211.       else /* DATA packet */
  212.           {/* write Buffer */
  213.            if(BatchFlag)
  214.                {if(FileSize<(long)PacketSize) i = (int) FileSize;
  215.                 else i = PacketSize;
  216.                 i = write(Handle,Buffer,i);
  217.                 FileSize -= (long)i;
  218.                }
  219.            else write(Handle,Buffer,PacketSize);
  220.           } /* end -- else */
  221.      } /* end -- for(p) */
  222. } /* end - RxyModem */
  223.  
  224. int TxCAN(int Port)
  225. {int i;
  226.  for(i=0;i<6;i++) PutChar(Port,CAN);
  227.  return(0);
  228. }